﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Utils.SoftReferences;
using GameObjects;

namespace WindowsGame.Ui
{
    class UiHex : VisibleObject
    {
        protected static Vector3 White = new Vector3(1f);

        public List<UiUnit> UiUnits = new List<UiUnit>();
        public List<UiPoint> UiPoints = new List<UiPoint>();

        public SoftRef<Hex> Hex { get; set; }
        public int UnitPositionMaker;

        public UiHex(UiModel uiModel) : base(uiModel)
        {
            uiModel.UiHexes.Add(this);
        }

        public override float Alpha
        {
            get
            {
                if (UiModel.HighlightedHexes.Count > 0 && !UiModel.HighlightedHexes.Contains(this))
                    return .4f;
                return 1f;
            }
        }

        public override Vector3 DiffuseColor { get; set; }

        public Matrix GetWorldTransform()
        {
            return GetHexWorldTransform(HexSpacing, Hex.Target.X, Hex.Target.Y);
        }

        const float HexSpacing = 3;
        const float MagicalHexagonConstant = 0.86602540378443864676372317075294f;

        private static Matrix GetHexWorldTransform(float hexSpacing, int i, int j)
        {
            return Matrix.CreateTranslation(GetXYForHexRowAndCol(i, j));
        }

        public static Vector3 GetXYForHexRowAndCol(int col, int row)
        {
            var x = (col / 2) * (HexSpacing) + (col % 2) * (HexSpacing / 2);
            var y = (Math.Abs(col % 2) * MagicalHexagonConstant) + (row * MagicalHexagonConstant * 2);

            return new Vector3(x, y, 0);
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            WorldTransform = GetHexWorldTransform(HexSpacing, Hex.Target.X, Hex.Target.Y);
        }
    }
}